#include <QApplication>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QDebug>
#include "src/widgets/HeadFiles.h"
#include "src/widgets/AddNoteBookDialog.h"
#include "src/widgets/AddNoteDialog.h"
#include "src/utils/utils.h"

void test_AddNoteBookDialog() {
    hps::AddNoteBookData data;
    hps::AddNoteBookDialog *addNoteBookDialog = new hps::AddNoteBookDialog(&data);
    addNoteBookDialog->show();
    if (addNoteBookDialog->exec() == QDialog::Accepted)
        std::cout << data._name << ", " << data._path << ", " << data._information << std::endl;
    else
        std::cout << "error" << std::endl;
}

void test_fileChoose() {
    // 打开文件选择对话框
//    QString filePath =
//        QFileDialog::getOpenFileName(nullptr, "Select File", QDir::homePath(), "All Files (*);;Text Files (*.txt)");
//
//    // 检查用户是否选择了文件
//    if (! filePath.isEmpty()) {
//        qDebug() << "Selected file path:" << filePath;
//    } else {
//        qDebug() << "No file selected.";
//    }
    // 打开文件夹选择对话框
    QString directoryPath = QFileDialog::getExistingDirectory(nullptr, "Select Directory", QDir::homePath());

    // 检查用户是否选择了目录
    if (! directoryPath.isEmpty()) {
        qDebug() << "Selected directory path:" << directoryPath;
    } else {
        qDebug() << "No directory selected.";
    }

}

class FilePickerWidget: public QWidget {
Q_OBJECT
public:
    FilePickerWidget(QWidget *parent = nullptr)
        : QWidget(parent) {
        // 创建文件路径显示的文本框和按钮
        filePathLineEdit = new QLineEdit(this);
        filePathLineEdit->setReadOnly(true);

        selectFileButton = new QPushButton("Select File", this);

        // 布局管理
        QHBoxLayout *layout = new QHBoxLayout(this);
        layout->addWidget(filePathLineEdit);
        layout->addWidget(selectFileButton);

        // 连接按钮的点击信号和槽函数
        connect(selectFileButton, &QPushButton::clicked, this, &FilePickerWidget::chooseFile);
    }

private slots:
    void chooseFile() {
        // 打开文件选择对话框
        QString filePath = QFileDialog::getOpenFileName(this, "Select File", QDir::homePath());

        // 如果用户选择了文件，更新文本框中的路径
        if (! filePath.isEmpty()) {
            filePathLineEdit->setText(filePath);
            qDebug() << "Selected file:" << filePath;
        }
    }

private:
    QLineEdit *filePathLineEdit;
    QPushButton *selectFileButton;
};

void test_fileChooser() {
    FilePickerWidget *filePickerWidget = new FilePickerWidget;
    filePickerWidget->show();
}

void test_addNote() {
    hps::AddNoteData data;
    hps::AddNoteDialog *dialog = new hps::AddNoteDialog(&data, "/home/hps/hps/Markdown-cpp/tests/TestData/book3");
    dialog->show();
}

void test_getDirName() {
    std::string path = "/home/hps/hps/Markdown-cpp/src/widgets/SideContentWindow.h";
    auto res = hps::getDirFromFilePath(path);
    std::cout << res << std::endl;
}

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
//    test_AddNoteBookDialog();
//    test_fileChoose();
//    test_fileChooser();
//    test_addNote();
    test_getDirName();
    return app.exec();
}
#include "test_demo.moc"